//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Summary description for AggregateGroupList.
	/// </summary>
	public class AggregateGroupList : WeblogBaseTemplatedWebControl
	{
		public AggregateGroupList()
		{
		}

        ResourceControl Title = null;
	    Repeater AggList = null;

        /// <summary>
        /// Property ResourceNameTitle (string)
        /// </summary>
        public string ResourceNameTitle
        {
            get
            {
                return ViewState["ResourceNameTitle"] as string;
            }
            set
            {  ViewState["ResourceNameTitle"] = value; }
        }

        /// <summary>
        /// Property UrlFormat (string)
        /// </summary>
        public string UrlFormat
        {
            get{  return ViewState["UrlFormat"] as string;}
            set{  ViewState["UrlFormat"] = value; }
        }

		private bool showOnlyMultipleGroups = false;
		public bool ShowOnlyMultipleGroups
		{
			get { return showOnlyMultipleGroups; }
			set { showOnlyMultipleGroups = value; }
		}

        public override void DataBind()
        {
            base.DataBind ();

            if(DataSource != null)
            {
				if (this.ShowOnlyMultipleGroups)
				{
					System.Collections.IEnumerable data = DataSource as System.Collections.IEnumerable;
					if (data != null)
					{
						System.Collections.IEnumerator enumerator = data.GetEnumerator();
						if (!enumerator.MoveNext() || !enumerator.MoveNext())
						{
							this.Visible = false;
							return;
						}
					}
				}

                AggList.DataSource = DataSource;
                AggList.DataBind();
            }

            this.Title.ResourceName = ResourceNameTitle;
            
        }

        private object _dataSource;
        /// <summary>
        /// Property DataSource (object)
        /// </summary>
        public object DataSource
        {
            get {  return this._dataSource; }
            set {  this._dataSource = value; }
        }

	    protected override void AttachChildControls()
	    {
	        Title = FindControl("Title") as ResourceControl;
            AggList = FindControl("AggList") as Repeater;
            AggList.ItemDataBound +=new RepeaterItemEventHandler(AggList_ItemDataBound);
        }

        private void AggList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Group g = e.Item.DataItem as Group;
                if(g != null)
                {
                    HyperLink aggLink = e.Item.FindControl("AggLink") as HyperLink;
                    if(aggLink !=null)
                    {
                        aggLink.Text = g.Name;
                        aggLink.NavigateUrl = Globals.GetSiteUrls().UrlData.FormatUrl(UrlFormat, g.GroupID);
                    }
                }
            }
        }
    }
}
